var MapIndexNew = {
	overlay:{
		areaOverlay:[],
		myOverlay:[],
		markerClustererLayer:null
	},
	map:null,
	init:function(){
		 var me =this;
		 me.layers = [];
		 var operation = sbl.getParameter("operation");
		 me.switchTopPabel(true);
		 me.switchRightPabel(true);
		 me.initLayout();
		 me.initBaseMap();
		 
		 me.layerDict = {
				"PoliceLayer":BaseLayer.newLayer({
					map:me.map,
					layerName:"PoliceLayer",
					dataUrl:burl+"/User/getModelsForMap.do",
					onMarkerClick:me.policeClick,
					isVisible:true
				}),
				"CameraLayer":BaseLayer.newLayer({
					map:me.map,
					layerName:"CameraLayer",
					dataUrl:burl+"/Camera/getModelsForMap.do",
					icon:{iconUrl:burl+"/images/camera/camera.png",width:18,height:20}
				})
		 };
		 
		 me.reloadMapOverlay();		 
		 me.initEvent();		
	},
	initLayout:function(){

	},
	initEvent:function(){
		var boss = this;
		
		$("#exit_session").bind("click", function(){
			sbl.ajax({
				url: burl + "/User/Exsit.do",
				data: {},
				success: function(d){
					alert(d.msg);
					window.location.href = burl + "/view/login.jsp";
				}
			});
		});
		
		$(".menuList li").bind("click",function(){
			if($(this).hasClass("selected")){
				$(this).removeClass("selected");
				var layerName = $(this).attr("tag");
				var layer = boss.layerDict[layerName];
				layer.setVisible(false);
			}
			else{
				$(this).addClass("selected");
				var layerName = $(this).attr("tag");
				var layer = boss.layerDict[layerName];
				layer.setVisible(true);
			}
		});
				
		$("#panel_switch").bind("click",function(){
			if($(this).hasClass("panel_open")){
				$(this).removeClass("panel_open");
				$(this).addClass("panel_close");
				boss.switchRightPabel(true);
			}
			else{
				$(this).removeClass("panel_class");
				$(this).addClass("panel_open");
				boss.switchRightPabel(false);
			}
		});
		
		$("#shouqi").bind("click",function(){
			if($(this).attr("close")){
				$(this).removeAttr("close");
				$(this).html("全屏");
				boss.switchTopPabel(true);
				boss.switchRightPabel(true);
			}
			else{
				$(this).attr("close","close");
				$(this).html("返回");
				boss.switchTopPabel(false);
				boss.switchRightPabel(false);
			}
		});
	},
	initBaseMap:function(zoom){
		var boss = this;
        if(!zoom){
        	zoom = 18;
        }
		boss.map = new BMap.Map("mapDiv");    
		boss.map.centerAndZoom(new BMap.Point(113.895367, 22.56343), zoom);
		boss.map.addControl(new BMap.MapTypeControl());
		boss.map.enableScrollWheelZoom();
		
		var myZoomCtrl = new MyMapControl(BMAP_ANCHOR_TOP_RIGHT,20, 40,"<div class='quanping' id='shouqi'>全屏</div>");
		boss.map.addControl(myZoomCtrl);
		
		boss.map.addEventListener("moveend", boss.reloadMapOverlay);
		boss.map.addEventListener("zoomend", function(){
			boss.reloadMapOverlay();
			boss.reloadAreaLayer();
			
		});
		
//		boss.map.addEventListener("click", function(e){
//			//alert(e.point.lng+","+e.point.lat);
//		});
		boss.map.boss = boss;
	},
	policeClick:function(overlay){
		alert("你单击的警员是："+overlay.data.NAME);
	},
	reloadMapOverlay:function(){
		var boss = MapIndexNew;
		if(!boss){
			boss = this;
		}
		for(var layerName in boss.layerDict){
			var layer = boss.layerDict[layerName];
			if(layer.getVisible()){
				layer.showLayer();
			}
		}
	},
	reloadAreaLayer:function(){
		var boss = MapIndexNew;
		var zoom = boss.map.getZoom();
		boss.clearAreaOverlay();
		boss.overlay.areaOverlay = [];
		
		var fullid = "0-1-";
		var isstreet = $("#xuanxiangka div[tag^='z'][selected]").last().attr("isstreet");
		var zoomLevel = boss.getZoomLevel();
		if(isstreet != "1"){
			if(zoomLevel == "baoan"){
				//宝安分局
				boss.loadAreaLayer("0-1-","2",fullid,null,"red");
			}
			else if(zoomLevel == "paichusuo"){
				//派出所
				boss.loadAreaLayer("0-1-","3",fullid,null,"red");
			}
			else if(zoomLevel == "jingqu"){
				//派出所和警区
				boss.loadAreaLayer("0-1-","4",fullid,null,"blue");
			}
			else if(zoomLevel == "zerenqu"){
				//派出所和责任区
				boss.loadAreaLayer("0-1-","5",fullid,null,"blue");
			}
		}
		else{
			if(zoom <= 14){
				//宝安分局
				boss.loadAreaLayer("0-1-","3",fullid,null,"red");
			}
			else {
				//街道办
				boss.loadAreaLayer("0-1-","4",fullid,null,"red");
			}
		}
	},
	loadAreaLayer:function(fullid,requeslevel,currentFullid,colorAttr,strokeColor){
		var boss = MapIndexNew;
		var bound = boss.map.getBounds();
		var miny = bound._swLat;
		var minx = bound._swLng;
		var maxy = bound._neLat;
		var maxx = bound._neLng;
		var isstreet = $("#xuanxiangka div[tag^='z'][selected]").last().attr("isstreet");
		var url = "";
		
		if(isstreet == "1"){
			url = burl+"/StreetDept/getOrgByLevel.do";
		}
		else{
			url = burl+"/Dept/getOrgByLevel.do"
		}
		
		sbl.ajax({
			dataType:'json',
			type:'get',
			data:{miny:miny,minx:minx,maxy:maxy,maxx:maxx,fullid:fullid,level:requeslevel},
			url:url,
			success:function(dd){
				var data;
				if(!dd.length){
					data = [dd];
				}
				else{
					data = dd;
				}
				var parentFullid = boss.getParentFullid(currentFullid);
				var selectedOrgLevel = boss.zoomLevelMapper[boss.getFullidLevel(currentFullid)];
				for(var i=0;i<data.length;i++){
					var d = data[i];
					var colorIndex = parseInt(d.fullid[d.fullid.length-2]);
					var points = [];
					var lonlats = d.mapxy.split(";");
					
					for(var j =0;j<lonlats.length;j++){
						var lonlat = lonlats[j].split(",");
						var lon = lonlat[0];
						var lat = lonlat[1];
						var point = new BMap.Point(lon,lat);
						points.push(point);
					}
					var fillColor = "gray";
					if(selectedOrgLevel > requeslevel){
						if(parentFullid.indexOf(d.fullid) > -1){
							fillColor = "";
						}
					}
					else if(selectedOrgLevel == requeslevel){
						if(d.fullid == currentFullid){
							fillColor = "";
						}
					}
					else if(selectedOrgLevel < requeslevel){
						if(d.fullid.indexOf(currentFullid)>-1){
							fillColor = "";
						}
					}
					
					var option={fillColor:fillColor,strokeWidth:"5px",fillOpacity:0.4,strokeStyle:"dashed",strokeColor:strokeColor};
					var polygon = new BMap.Polygon(points,option);
					boss.map.addOverlay(polygon);
					polygon.setStrokeWeight(4);
					boss.overlay.areaOverlay.push(polygon);
				}
			}
		});
	},
	clearAreaOverlay:function(){
		var boss = this;
		
		for(var i in boss.overlay.areaOverlay){
			boss.map.removeOverlay(boss.overlay.areaOverlay[i]);
		}
	},
	switchRightPabel:function(isOpen){
		var pabelWidth = 212;
		var bodyWidth = document.documentElement.clientWidth;
		var mainWidth = 0;
		
		if(isOpen){
			mainWidth = bodyWidth - pabelWidth - 2;
			$("#filter").show();
		}
		else{
			mainWidth = bodyWidth;
			$("#filter").hide();
		}
		
		$(".main").css("width", mainWidth);
		$(".container").css("width",bodyWidth);
	},
	switchTopPabel:function(isOpen){
		var pabelHeight = 110;
		var bodyHeight = document.documentElement.clientHeight;
		var mainHeight = 0;
		
		if(isOpen){
			mainHeight = bodyHeight - pabelHeight;
			$(".header").show();
		}
		else{
			mainHeight = bodyHeight;
			$(".header").hide();
		}
		
		$(".main,#filter").css("height", mainHeight);
	},
	getParentFullid:function(fullid){
		var fullids = fullid.split("-");
		var result = "";
		
		if(fullids.length > 3){
			for(var i=0;i<fullids.length-2;i++){
				result += fullids[i]+"-"
			}
		}
		return result;
	},
	getZoomLevel:function(){
		var boss = this;
		var zoom = boss.map.getZoom();
		
		if(zoom <= 14){
			return "baoan";
		}
		else if(zoom == 15){
			return "paichusuo";
		}
		else if(zoom == 16){
			return "jingqu";
		}
		else {
			return "zerenqu";
		}
	},
	zoomLevelMapper:{
		baoan:2,
		paichusuo:3,
		jingqu:4,
		zerenqu:5
	},
	getFullidLevel:function(fullid){
		var infos = fullid.split("-");
		if(infos.length == 3){
			return "baoan";
		}
		else if(infos.length == 4){
			return "paichusuo";
		}
		else if(infos.length == 5){
			return "jingqu";
		}
		else if(infos.length == 6){
			return "zerenqu";
		}
	}
};
$(function(){
	MapIndexNew.init();
});